/**
 * 
 */
package top.jle.jpa.basic.dao;

import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import org.hibernate.query.criteria.HibernateCriteriaBuilder;

import java.io.Serial;
import java.util.Arrays;
import java.util.Objects;


/**
 * 将多个过滤器用or连接成新的过滤器
 * 
 * @author jim.lee
 *
 */
public final class OrPreFilter implements PreFilter {
	
	
	
	/**
	 * 
	 */
	@Serial
	private static final long serialVersionUID = 3454224360136574616L;
	private final PreFilter[] preFilters;
	
	OrPreFilter(PreFilter[] preFilters){
		if(preFilters==null || preFilters.length<2){
			throw new IllegalArgumentException("length of preFilters must be greater than or equal 2");
		}
		this.preFilters =preFilters;
	}


	/* (non-Javadoc)
	 * @see com.emin.eminview.base.dao.PreFilter#buildCriterion(org.hibernate.criterion.DetachedCriteria)
	 */
	@Override
	public <T> Predicate buildCriterion(Root<T> root, HibernateCriteriaBuilder builder) {

		return builder.or(Arrays.stream(preFilters).filter(Objects::nonNull).map(preFilter -> preFilter.buildCriterion(root,builder)).toArray(Predicate[]::new));
	}

}
